package com.kum.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kum.domain.entity.SysSign;
import com.kum.domain.entity.SysUser;
import com.kum.mapper.SysSignMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @version V1.0
 * @Package com.kum.service
 * @auhter 枯木Kum
 * @date 2021/3/10-1:18 PM
 */

@Slf4j
@Service
public class SysSignService {

    @Resource
    private SysSignMapper sysSignMapper;

    public List<SysSign> list(){
        return sysSignMapper.selectList(null);
    }
    public void save(SysSign sysSign){
        SysUser sysUser = sysSign.getSysUser();
        sysSign.setUserId(sysUser.getId());
        sysSign.setUserName(sysUser.getName());
        if(sysSign.getId() == null){
            sysSignMapper.insert(sysSign);
            return;
        }
        sysSignMapper.updateById(sysSign);
    }

    /**
     * 根据时间区间进行检索
     * @param start 开始时间
     * @param end 结束时间
     * @return
     */
    public List<SysSign> findByStartAndEndDate(String start, String end){
        log.info("start time {} & end time {}",start,end);
        QueryWrapper<SysSign> wrapper = new QueryWrapper<SysSign>().
                apply("date_format (sign_date,'%Y-%m-%d') >= date_format('" + start + "','%Y-%m-%d')").
                apply("date_format (sign_date,'%Y-%m-%d') <= date_format('" + end + "','%Y-%m-%d')");
       return sysSignMapper.selectList(wrapper);

    }


}
